package com.qf.fmall2302.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;

@Component
@ServerEndpoint("/test/{id}")
public class VoteWebSocketServer {

    public static HashMap<String,Session> sessionHashMap
            = new HashMap<String,Session>();

    @OnOpen
    public void onOpen(Session session,@PathParam("id") String id){
        System.out.println("页面建立websocket连接：id="+id);
        sessionHashMap.put(id,session);
    }

    @OnClose
    public void onClose(Session session,@PathParam("id") String id) throws IOException {
        System.out.println("页面断开websocket连接：id="+id);
        session.close(); // 关闭session'对象
        sessionHashMap.remove(id);  // 删除建立websocket连接时保持的session对象
    }


}
